package com.vesystem.vegua.config;

import com.vesystem.vegua.modules.common.dto.TunnelDto;
import com.vesystem.vegua.websocket.WebSocketTunnel;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

/**
 * @Author: hcy
 * @Date: 2020/12/21 22:47
 */
@Configuration
@EnableScheduling
public class ServeTimer {
    private static Logger log = LoggerFactory.getLogger(ServeTimer.class);

    /**
     * 定时清除已经断开的连接
     */
    @Scheduled(fixedRate = 1000 * 60 *30)//30分钟清理一次
    private void checkTunnel(){
        log.debug( "大小："+WebSocketTunnel.guacamoleTunnelMap.size()+" >内容:" + WebSocketTunnel.guacamoleTunnelMap.toString());
        Set<String> keySet = WebSocketTunnel.guacamoleTunnelMap.keySet();
        for (String t:keySet){
            if ( WebSocketTunnel.guacamoleTunnelMap.get(t) !=null &&  WebSocketTunnel.guacamoleTunnelMap.get(t).getGuacamoleTunnel() !=null ){
                if ( !WebSocketTunnel.guacamoleTunnelMap.get(t).getGuacamoleTunnel().isOpen() ){
                    WebSocketTunnel.guacamoleTunnelMap.remove(t);
                }
            }
        }
    }



}
